// blocking.js
// Implement 'blacklist' function by blocking things specified by the user.
// This file also does much that should have been done by customui.js for
// efficiency reasons.
// Adapted from bbs.stuhome.net.blockspecificuser.complete.user.js
// matches: / /index.php* /thread.php* /read.php*

function getFirstChildByClassName(parentElement, className)
{
	for(var i = 0; i < parentElement.children.length; ++i)
		if(parentElement.children[i].className == className)
			return parentElement.children[i];
	return null;
}

getOptions(["Blocking.PostsByUsers", "Blocking.SignaturesOfUsers", 
	"Blocking.PhotosOfUsers", "Blocking.ImagesInPostsByUsers", 
	"Blocking.PostsFromForums", 
	"UI.HideAuthorsInHotlist", "UI.SimpleRead"], 
	(function(options)
{
var block_users = options.PostsByUsers || [];
var block_signature_users = options.SignaturesOfUsers || [];
var block_photos = options.PhotosOfUsers || [];
var block_images = options.ImagesInPostsByUsers || [];
var block_forums = options.PostsFromForums || [];
var hide_authors = options.HideAuthorsInHotlist || false;
var simple_read = options.SimpleRead || false;
var block_content = [];

//Modify the hot list on homepage
(location.pathname == "/" || location.pathname == "/index.php") && 
(block_users.length || block_forums.length || hide_authors) && (function ()
{
	var list = document.getElementsByClassName("top"); //<ul>
	function blockPostInToplist(number, link, author)
	{
		link.innerHTML = "";
		var new_link = link.cloneNode();
		new_link.style.display = "";
		new_link.style.width = number.style.width;
		link.parentElement.insertBefore(new_link, link);
		new_link.appendChild(number);
		author.style.display = "none";
	}
	var filterPosts = function(ul)
	{
		for(var i = 0; i < ul.children.length; ++i)
		{
			var cur_li = ul.children[i];
			var cur_number = cur_li.children[0];
			var cur_link = cur_li.children[1];
			var cur_author = cur_li.children[2];
			if(hide_authors && cur_author)
			{
				cur_author.style.display = "none";
				cur_link.style.height = "20px";
				cur_link.style.width = "300px";
				cur_link.style.overflow = "hidden";
				var title = (cur_link.innerText || cur_link.textContent);
				if(cur_link.title)
					title = cur_link.title.substr(0, cur_link.title.indexOf("\n"));
				cur_link.innerText = cur_link.textContent = title;
			}
			for(var j = 0; j < block_users.length; ++j)
			{
				var block = false;
				if(cur_link.title && 
				(cur_link.title.indexOf("作者:" + block_users[j]) != -1 || 
				cur_link.title.indexOf("作者: " + block_users[j]) != -1) )
					block = true;
				else
				{
					var author_with_brackets = (cur_author.innerText || cur_author.textContent);
					var test = /^\[(.*?)\]$/;
					if(author_with_brackets && 
						test.test(author_with_brackets) && 
						block_users[j] == (author_with_brackets).match(
							test)[1])
					block = true;
				}
				if(block)
					blockPostInToplist(cur_number, cur_link, cur_author);
			}
			var forum = (cur_link.title.match(/版区:(?:<.*?>)?([^<\s]+)/) 
				|| ["", ""])[1];
			if(block_forums.indexOf(forum) != -1)
				blockPostInToplist(cur_number, cur_link, cur_author);
		}
	}
	
	filterPosts(list[0]); //最新回复主题
	filterPosts(list[1]); //最新发表主题
	filterPosts(list[3]); //好友最新主题
})();

//Blocking users in thread list
(location.pathname == "/thread.php") && block_users.length && (function ()
{
	var hide_authors = true;
	var list = document.getElementsByClassName("tr3 t_one"); //<tr>
	for(var i = 0; i < list.length; ++i)
	{
		var td = list[i].children[2];
		var a = td.children[0];
		var text = a.innerText || a.textContent;
		for(var j = 0; j < block_users.length; ++j)
			if(text == block_users[j])
			{
				list[i].style.display = "none";
				break;
			}
	}
})();

//Modify post reading pages
(location.pathname == "/read.php") && 
(block_users.length || block_signature_users.length || 
block_photos.length || block_images.length || block_content.length ||
simple_read) && 
(function ()
{
	var dummy = {style:{}};
	var posts = document.getElementsByClassName("t5");
	for(var i = 0; i < posts.length; ++i)
	{
		var div = posts[i];
		var tbody = div.children[0].children[0];
		//                  table       tbody
		var content_tr = tbody.children[0];
		//                          tr.tr1
		if(content_tr.className != "tr1")
			continue;
		
		var userinfo_th = content_tr.children[0];
		//                              th.r_two
		var username_div = userinfo_th.children[0] || dummy;
		var userhonor_div = getFirstChildByClassName(userinfo_th, "honor") || dummy;
		var userpic_div = getFirstChildByClassName(userinfo_th, "user-pic") || dummy;
		var userdetails_div = 
			(userpic_div || userhonor_div || username_div).
			nextElementSibling || dummy;

		var signature_th = tbody.children[1].children[0];
		//                         tr1 r_one         th
		var signature_line = getFirstChildByClassName(signature_th, "sigline") || dummy;
		var signature_content = getFirstChildByClassName(signature_th, "signature") || dummy;
		var post_actions = getFirstChildByClassName(signature_th, "tipad black") || dummy;
		if(simple_read)
		{
			userpic_div.style.display = "none";
				userhonor_div.style.display = 
				userdetails_div.style.display = 
				signature_line.style.display = 
				signature_content.style.display = "none";
			signature_th.style.paddingTop = "0px";
			post_actions.style.margin = "0 1em 0 1em";
			var username_text_div = getFirstChildByClassName(username_div, "readmagic");
			var username_a = username_text_div.children[0].children[0];
			username_a.style.backgroundColor = "";
		}
		var user = username_div.children[1];
		//                  div.listread.fl
		var username = "";
		if(user)
		{
			username = user.innerText || user.textContent;
			if(block_users.indexOf(username) != -1)
			{
				div.style.display = "none";
				continue;
			}
			if(block_signature_users.indexOf(username) != -1)
				signature_content.style.display = "none";
			if(block_photos.indexOf(username) != -1)
			{
				var photo_container = content_tr.children[0];
				for(var j = 0; j < photo_container.children.length; ++j)
				{
					if(photo_container.children[j].className == "user-pic")
					{
						var img = photo_container.children[j].children[0].
							children[0].children[0].children[0].children[0].
							children[0];
						img.style.display = "none";
						break;
					}
				}
			}
		}

		if(block_images.length == 0 && block_content.length == 0)
			continue;
		//Blocking content or images in content
		var post_th = content_tr.children[1];
		//                      th#td_xxx.r_one
		for(var j = 0; j < post_th.children.length; ++j)
			if(post_th.children[j].className == "tpc_content")
			{
				if(block_images.indexOf(username) != -1)
				{
					function hideImages(element)
					{
						for(var i = 0; i < element.children.length; ++i)
						{
							var cur = element.children[i];
							if(cur.tagName.toLowerCase() == "img")
								cur.style.display = "none";
							else
								hideImages(cur);
						}
					}
					hideImages(post_th.children[j]);
				}

				if(block_content.length)
				{
					var cur = post_th.children[j];
					var text = ( (cur.innerText || cur.textContent).match(
						/[\u4e00-\u9fa5]/g) || [] ).join('');
					for(var k = 0; k < block_content.length; ++k)
					{
						if(text == block_content[k])
						{
							div.style.display = "none";
							break;
						}
					}
				}
				break;
			}
	}
})();

}));
